#include "campaign.h"
#include "campaignitems.h"


Campaign::Campaign(QObject *parent)
    : DataRoot(parent),
    SKILLS_TYPE(qRegisterMetaType<CampaignSkills>("CampaignSkills")),
    FEATS_TYPE(qRegisterMetaType<CampaignFeats>("CampaignFeats")),
    SPECIALS_TYPE(qRegisterMetaType<CampaignSpecials>("CampaignSpecials")),
    CLASSES_TYPE(qRegisterMetaType<CharacterClasses>("CharacterClasses"))
{
    qRegisterMetaType<Campaign::Attribute>("Attribute");
    QTimer::singleShot(0, this, SLOT(onInitialize()));
}

Campaign::Campaign(const Campaign &other) :
    DataRoot(other),
    SKILLS_TYPE(other.SKILLS_TYPE),
    FEATS_TYPE(other.FEATS_TYPE),
    SPECIALS_TYPE(other.SPECIALS_TYPE),
    CLASSES_TYPE(other.CLASSES_TYPE),
    m_classes(other.m_classes),
    m_skills(other.m_skills),
    m_feats(other.m_feats),
    m_specials(other.m_specials)
{
}

CharacterClasses *Campaign::classes() const
{
    return qobject_cast<CharacterClasses*>(m_classes.data());
}

CampaignSkills *Campaign::skills() const
{
    return qobject_cast<CampaignSkills*>(m_skills.data());
}

CampaignFeats *Campaign::feats() const
{
    return qobject_cast<CampaignFeats*>(m_feats.data());
}

CampaignSpecials *Campaign::specials() const
{
    return qobject_cast<CampaignSpecials*>(m_specials.data());
}

DataSuite *Campaign::suite(int type) const
{
    if (type == SKILLS_TYPE)    return m_skills;
    if (type == FEATS_TYPE)     return m_feats;
    if (type == SPECIALS_TYPE)  return m_specials;
    if (type == CLASSES_TYPE)   return m_classes;

    return 0;
}

QList<int> Campaign::types() const
{
    QList<int> ret;
    ret << SKILLS_TYPE;
    ret << FEATS_TYPE;
    ret << SPECIALS_TYPE;
    ret << CLASSES_TYPE;
    return ret;
}


const Campaign *Campaign::get(DataContainer *container)
{
    return qobject_cast<const Campaign*>(container->root());
}

void Campaign::onInitialize()
{
    static bool DONE = false;
    if (DONE) return;

//    qDebug() << Q_FUNC_INFO;
    if (m_classes.isNull())
    {
        m_classes = findChild<CharacterClasses*>();
        if (m_classes.isNull())
        {
            m_classes = qobject_cast<DataSuite*>(create(CLASSES_TYPE, this));
        }
    }

    if (m_skills.isNull())
    {
        m_skills = findChild<CampaignSkills*>();
        if (m_skills.isNull())
        {
            m_skills = qobject_cast<DataSuite*>(create(SKILLS_TYPE, this));
        }
    }

    if (m_feats.isNull())
    {
        m_feats = findChild<CampaignFeats*>();
        if (m_feats.isNull())
        {
            m_feats = qobject_cast<DataSuite*>(create(FEATS_TYPE, this));
        }
    }

    if (m_specials.isNull())
    {
        m_specials = findChild<CampaignSpecials*>();
        if (m_specials.isNull())
        {
            m_specials = qobject_cast<DataSuite*>(create(SPECIALS_TYPE, this));
        }
    }

    DONE = true;
}

DataContainer *CampaignSuiteItem::createChild()
{
    int fields = root()->fieldCount();

    DataContainer * child = qobject_cast<DataContainer*>(CampaignSuiteItem::create(CHILD_TYPE, this));
    if (!child) return 0;

    QString name = QString("[%1]").arg(QMetaType::typeName(CHILD_TYPE));
    child->setName(name);
    if (child->fieldCount() > fields)
    {
        emit sigFieldCountChanged();
    }

    return child;
}

